﻿<?php

/*
 *
 *
 * --------------------------------------------------------------------
 * Copyright (c) 2001 - 2011 Openfiler Project.
 * --------------------------------------------------------------------
 *
 * Openfiler is an Open Source SAN/NAS Appliance Software Distribution
 *
 * This file is part of Openfiler.
 *
 * Openfiler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Openfiler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Openfiler.  If not, see <http://www.gnu.org/licenses/>.
 *
 * --------------------------------------------------------------------
 *
 *
 */

    require("pre.inc");
    require("md.inc");
    include("authenticated.inc");

    $md = get_md();

    $disksp = popen("export LANG=C; /usr/bin/sudo /opt/openfiler/sbin/list-disks.pl", "r");
    $i = 0;
    while (!feof($disksp))
    {
            $dstr = trim(fgets($disksp, 4096));
            // $dresult[$i++] = substr($dstr, 0, strlen($dstr) - 1);
            $dresult[$i++] = $dstr;
    }
    pclose($disksp);

    $of_disks = array();

    if ($dresult[0] == "OPENFILER")
    {
            $i = 1;

            while ($i < count($dresult))
            {
                    if (strncmp($dresult[$i], "DISK", 4) == 0)
                    {
                            $p = $i;
                            $i = $i + 8;

                            $parts = array();

                            while (($dresult[$i] == "PARTITION") && ($i < count($dresult)))
                            {
                                    array_push($parts, array("device" => $dresult[$i + 1], "type" => $dresult[$i + 2], "number" => $dresult[$i + 3], "start" => $dresult[$i + 4], "end" => $dresult[$i + 5], "blocks" => $dresult[$i + 6], "extended" => $dresult[$i + 7]));
                                    $i = $i + 8;
                            }

                            array_push($of_disks,
                                    array("label" => substr($dresult[$p], 5, strlen($dresult[$p]) - 5),
                                            "device" => $dresult[$p + 1],
                                            "type" => $dresult[$p + 2],
                                            "description" => $dresult[$p + 3],
                                            "blocksize" => ($dresult[$p + 4] * 2),
                                            "cylinders" => $dresult[$p + 5],
                                            "heads" => $dresult[$p + 6],
                                            "sectors" => $dresult[$p + 7],
                                            "partitions" => $parts));
                    }
                    else
                        break;
            }
    }

    $found = false;
    $disk_index = -1;

    for ($i = 0; $i < count($of_disks); $i++)
    {
            if ($of_disks[$i]["device"] == $dev)
            {
                    $found = true;
                    $disk_index = $i;
                    break;
            }
    }

    if (!$found)
    {
            header("Location: ./volumes_physical.html");
            exit;
    }

    $extended_partitions_exist = false;
    $in_use = array();

    for ($j = 0; $j < count($of_disks[$disk_index]["partitions"]); $j++)
    {
            $ptype = $of_disks[$disk_index]["partitions"][$j]["type"];
            $pv_device = $of_disks[$disk_index]["partitions"][$j]["device"];

            if (($of_disks[$disk_index]["label"] != "gpt") && (floatval($of_disks[$disk_index]["partitions"][$j]["number"]) > 4))
                    $extended_partitions_exist = true;

            $in_use[$j] = FALSE;

            if ($ptype == "8e")
            {
                    $pvp = popen("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "pvdisplay " . $pv_device, "r");
                    $pvstr = trim(fgets($pvp, 4096));
                    pclose($pvp);

                    if ((strcmp($pvstr, "--- Physical volume ---") == 0) || (strcmp($pvstr, "--- NEW Physical volume ---") == 0))
                    {
                            $pvp = popen("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "pvdisplay -c " . $pv_device, "r");
                            $pvstr = trim(fgets($pvp, 4096));
                            pclose($pvp);

                            $pvinfo = explode(":", trim($pvstr, " "));

                            if (($pvinfo[0] == $pv_device) && (strlen($pvinfo[1]) > 0))
                                    $in_use[$j] = true;
                    }
            }
            else if ($ptype == "fd")
            {
                    for ($ii = 0; $ii < count($md); $ii++)
                    {
                            for ($i = 0; $i < count($md[$ii]["members"]); $i++)
                            {
                                    if ($md[$ii]["members"][$i]["device"] == $pv_device)
                                    {
                                            $in_use[$j] = true;
                                            break;
                                    }
                            }

                            if ($in_use[$j])
                                    break;
                    }
            }
    }

    if ($action == "deletepartition")
    {
            for ($j = 0; $j < count($of_disks[$disk_index]["partitions"]); $j++)
            {
                    if ($of_disks[$disk_index]["partitions"][$j]["device"] == $device)
                    {
                            $ptype = $of_disks[$disk_index]["partitions"][$j]["type"] . "";

                            if ((($ptype != "8e") && ($ptype != "fd") && ($ptype != "05") && ($ptype != "0f") && ($ptype != "5") && ($ptype != "f")))
                                    continue;
                            else if ((($ptype == "05") || ($ptype == "0f") || ($ptype == "5") || ($ptype == "f")) && ($extended_partitions_exist))
                                    continue;
                            else if ($in_use[$j])
                                    continue;
                            exec("export LANG=C; /usr/bin/sudo /usr/sbin/parted -s " . escapeshellarg($dev) . " rm " . escapeshellarg($of_disks[$disk_index]["partitions"][$j]["number"]));
                            //exec("export LANG=C; /usr/bin/sudo /usr/sbin/partprobe 2>&1");
                    }
            }

            header("Location: ./volumes_editpartitions.html?dev=" . urlencode($dev));
            exit;
    }

    if ($action == "resetpartitions")
    {
            exec("export LANG=C; /usr/bin/sudo /bin/dd if=/dev/zero of=" . escapeshellarg($dev) ." bs=512 count=1000 ; sleep 2");
            //exec("export LANG=C; /usr/bin/sudo /bin/dd if=/dev/zero of=" . escapeshellarg($dev) ." bs=512 count=1000 1>/dev/null 2>/dev/null ; sleep 2");
           // exec("export LANG=C; /usr/bin/sudo /usr/sbin/partprobe");
            header("Location: ./volumes_editpartitions.html?dev=" . urlencode($dev));
            exit;
    }

    else if ($action == "createpartition")
    {
            $start_mb = floatval((($start - 1) * ($of_disks[$disk_index]["blocksize"] / 2) * $of_disks[$disk_index]["sectors"] * $of_disks[$disk_index]["heads"]) / (1024 * 1024.0));
            $end_mb = floatval((($end) * ($of_disks[$disk_index]["blocksize"] / 2) * $of_disks[$disk_index]["sectors"] * $of_disks[$disk_index]["heads"]) / (1024 * 1024.0)) - 1;

            $parttablep = popen("export LANG=C; /usr/bin/sudo /usr/sbin/parted " . escapeshellarg($dev) . " print", "r");
            $i = 0;
            while (!feof($parttablep))
            {
                    $ptstr = trim(fgets($parttablep, 4096));
                    $ptresult[$i++] = $ptstr;
            }
            pclose($parttablep);

            if (strstr($ptresult[0], "unrecognised disk label"))
            {
                    exec("export LANG=C; /usr/bin/sudo /usr/sbin/parted -s " . escapeshellarg($dev) . " mklabel gpt");
                   // exec("export LANG=C; /usr/bin/sudo /usr/sbin/partprobe 2>&1");
            }

            if (($mode == "primary") && ($type == "extended"))
            {
                    exec("export LANG=C; /usr/bin/sudo /usr/sbin/parted -s " . escapeshellarg($dev) . " mkpart extended " . escapeshellarg($start_mb) . " " . escapeshellarg($end_mb));
                   // exec("export LANG=C; /usr/bin/sudo /usr/sbin/partprobe 2>&1");
            }
            else if ($type != "extended")
            {
                    exec("export LANG=C; /usr/bin/sudo /usr/sbin/parted -s " . escapeshellarg($dev) . " mkpart " . escapeshellarg($mode) . " " . escapeshellarg($start_mb) . " " . escapeshellarg($end_mb));
                   // exec("export LANG=C; /usr/bin/sudo /usr/sbin/partprobe 2>&1");

                    $disksp = popen("export LANG=C; /usr/bin/sudo /opt/openfiler/sbin/list-disks.pl", "r");
                    $i = 0;
                    while (!feof($disksp))
                    {
                            $dstr = trim(fgets($disksp, 4096));
                            // $dresult[$i++] = substr($dstr, 0, strlen($dstr) - 1);
                            $dresult[$i++] = $dstr;
                    }
                    pclose($disksp);

                    $of_disks2 = array();

                    if ($dresult[0] == "OPENFILER")
                    {
                            $i = 1;

                            while ($i < count($dresult))
                            {
                                    if (strncmp($dresult[$i], "DISK", 4) == 0)
                                    {
                                            $p = $i;
                                            $i = $i + 8;

                                            $parts = array();

                                            while (($dresult[$i] == "PARTITION") && ($i < count($dresult)))
                                            {
                                                    array_push($parts, array("device" => $dresult[$i + 1], "type" => $dresult[$i + 2], "number" => $dresult[$i + 3], "start" => $dresult[$i + 4], "end" => $dresult[$i + 5], "blocks" => $dresult[$i + 6], "extended" => $dresult[$i + 7]));
                                                    $i = $i + 8;
                                            }

                                            array_push($of_disks2,
                                                    array("label" => substr($dresult[$p], 5, strlen($dresult[$p]) - 5),
                                                            "device" => $dresult[$p + 1],
                                                            "type" => $dresult[$p + 2],
                                                            "description" => $dresult[$p + 3],
                                                            "blocksize" => ($dresult[$p + 4] * 2),
                                                            "cylinders" => $dresult[$p + 5],
                                                            "heads" => $dresult[$p + 6],
                                                            "sectors" => $dresult[$p + 7],
                                                            "partitions" => $parts));
                                    }
                                    else
                                            break;
                            }
                    }

                    $found = false;
                    $disk_index2 = -1;

                    for ($i = 0; $i < count($of_disks2); $i++)
                    {
                            if ($of_disks2[$i]["device"] == $dev)
                            {
                                    $found = true;
                                    $disk_index2 = $i;
                                    break;
                            }
                    }

                    if (!$found)
                    {
                            header("Location: ./volumes_physical.html");
                            exit;
                    }

                    for ($i = 0; $i < count($of_disks2[$disk_index2]["partitions"]); $i++)
                    {
                            /* Got to check the extents and not the partition numbers */

                            if (($of_disks2[$disk_index2]["partitions"][$i]["type"] == "5") || ($of_disks2[$disk_index2]["partitions"][$i]["type"] == "05") || ($of_disks2[$disk_index2]["partitions"][$i]["type"] == "f") || ($of_disks2[$disk_index2]["partitions"][$i]["type"] == "0f"))
                                            continue;

                            $found = false;

                            for ($j = 0; $j < count($of_disks[$disk_index]["partitions"]); $j++)
                            {
                                    if (($of_disks[$disk_index]["partitions"][$j]["type"] == "5") || ($of_disks[$disk_index]["partitions"][$j]["type"] == "05") || ($of_disks[$disk_index]["partitions"][$j]["type"] == "f") || ($of_disks[$disk_index]["partitions"][$j]["type"] == "0f"))
                                            continue;

                                    if (($of_disks2[$disk_index2]["partitions"][$i]["start"] == $of_disks[$disk_index]["partitions"][$j]["start"]) &&
                                             ($of_disks2[$disk_index2]["partitions"][$i]["end"] == $of_disks[$disk_index]["partitions"][$j]["end"]))
                                    {
                                            $found = true;
                                            break;
                                    }
                            }

                            if (!$found)
                            {
                                    if ($type == "pv")
                                            $part_flag = "lvm";
                                    else
                                            $part_flag = "raid";

                                    exec("export LANG=C; /usr/bin/sudo /usr/sbin/parted -s " . escapeshellarg($dev) . " set " . escapeshellarg($of_disks2[$disk_index2]["partitions"][$i]["number"]) . " " . $part_flag . " on");

                                    //exec("export LANG=C; /usr/bin/sudo /usr/sbin/partprobe 2>&1");

                                    sleep(2);  // give the OS time to reread partition tables and recreate device entries
                                               // also prevent race condition with LVM

                                    if ($type == "pv")
                                            exec("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "pvcreate " . escapeshellarg($of_disks2[$disk_index2]["partitions"][$i]["device"]));

                                    //exec("export LANG=C; /usr/bin/sudo /usr/sbin/partprobe 2>&1");

                                    break;
                            }
                    }
            }

            header("Location: ./volumes_editpartitions.html?dev=" . urlencode($dev));
            exit;
    }

    generic_header(array("title" => _("OPENFILER : 存储卷 : 磁盘设备 : 编辑分区")));
    single_begin(array());
?>

<script src="utility.js" type="text/javascript" language="JavaScript">
</script>
<script src="popup.js" type="text/javascript" language="JavaScript">
</script>

<p>&nbsp;</p>

<?php
        nested_tab_begin("C_VOLUME_PHYSICAL_MANAGEMENT");
	print("<h3 align=\"center\">编辑分区 " . $of_disks[$disk_index]["device"] . " (" . ($of_disks[$disk_index]["cylinders"]) . " 柱面，标签 \"" . $of_disks[$disk_index]["label"] . "\" )</h3>\n");

	print("<p align=\"center\">&nbsp;</p>\n");

	print("<div align=\"center\">\n");
	print("<table cellpadding=\"8\" cellspacing=\"2\" border=\"0\">\n");
	print("<tr>\n");
	print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>设备</strong></td>\n");
	print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"left\"><strong>类型</strong></td>\n");
	print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>序号</strong></td>\n");
	print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>开始柱面</strong></td>\n");
	print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>结束柱面</strong></td>\n");
	print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>块数</strong></td>\n");
	print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>大小</strong></td>\n");
	print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>类型</strong></td>\n");
	print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>删除</strong></td>\n");
	print("</tr>\n");

	$primaries = 0;
	$primary_cylinder_count = 0;
	$extended_cylinder_count = 0;
	$extended_partition_index = -1;
	$primary_start_array = array();
	$primary_end_array = array();
	$extended_start_array = array();
	$extended_end_array = array();

        $jsondata_size = array();
        $jsondata_label = array();
        $sumpartsize = 0;  // sum of all partitions
        $disksize = floatval($of_disks[$disk_index]["cylinders"]) * floatval($of_disks[$disk_index]["sectors"]) *
                           floatval($of_disks[$disk_index]["heads"]) * floatval($of_disks[$disk_index]["blocksize"]) / 2;


	for ($j = 0; $j < count($of_disks[$disk_index]["partitions"]); $j++)
	{


		if ($dcolor == 0)
		{
			$dcolor = 1;
			$dvalue = $GLOBALS["color_table_row1"];
		}
		else
		{
			$dcolor = 0;
			$dvalue = $GLOBALS["color_table_row2"];
		}

		$ptype = $of_disks[$disk_index]["partitions"][$j]["type"] . "";

		if (($of_disks[$disk_index]["label"] != "gpt") && (($ptype == "05") || ($ptype == "5") || ($ptype == "0f") || ($ptype == "f")))
			$extended_partition_index = $j;

		if (($of_disks[$disk_index]["label"] == "gpt") || ($of_disks[$disk_index]["partitions"][$j]["number"] <= 4))
		{
			array_push($primary_start_array, $of_disks[$disk_index]["partitions"][$j]["start"]);
			array_push($primary_end_array, $of_disks[$disk_index]["partitions"][$j]["end"]);
			$primary_cylinder_count = $primary_cylinder_count + ($of_disks[$disk_index]["partitions"][$j]["end"] - $of_disks[$disk_index]["partitions"][$j]["start"] + 1);
			$primaries += (1 << (floatval($of_disks[$disk_index]["partitions"][$j]["number"]) - 1));
		}
		else
		{
			array_push($extended_start_array, $of_disks[$disk_index]["partitions"][$j]["start"]);
			array_push($extended_end_array, $of_disks[$disk_index]["partitions"][$j]["end"]);
			$extended_cylinder_count = $extended_cylinder_count + ($of_disks[$disk_index]["partitions"][$j]["end"] - $of_disks[$disk_index]["partitions"][$j]["start"] + 1);
		}

		switch ($ptype)
		{
			case "83":	$ptype_str = "Linux Filesystem (<tt>0x" . $ptype . "</tt>)"; break;
			case "82":	$ptype_str = "Linux Swap (<tt>0x" . $ptype . "</tt>)"; break;
			case "8e":	$ptype_str = "Linux Physical Volume (<tt>0x" . $ptype . "</tt>)"; break;
			case "fd":	$ptype_str = "Linux RAID Array Member (<tt>0x" . $ptype . "</tt>)"; break;

			case "05":
			case "5":
			case "0f":
			case "f":	$ptype_str = "Extended Partition (<tt>0x" . $ptype . "</tt>)"; break;

			default:	$ptype_str = "Unknown Partition Type (<tt>0x" . $ptype . "</tt>)"; break;
		}

	    
                $tmpH = popen('export LANG=C; /usr/bin/sudo /sbin/blockdev --getsize64 2>&1 ' . $of_disks[$disk_index]["partitions"][$j]["device"] , 'r');
                $part_size = trim(fread($tmpH, 2096));
                pclose($tmpH);
                
                

                $sumpartsize += $part_size;

                if ($part_size > (1024 * 1024 * 1024 * 1024))
			$part_size_string = sprintf("%.02f TB", ($part_size / (1024 * 1024 * 1024 * 1024.0)));
		else if ($part_size > (1024 * 1024 * 1024))
			$part_size_string = sprintf("%.02f GB", ($part_size / (1024 * 1024 * 1024.0)));
		else if ($part_size > (1024 * 1024))
			$part_size_string = sprintf("%.02f MB", ($part_size / (1024 * 1024.0)));
		else if ($part_size > (1024))
			$part_size_string = sprintf("%.02f KB", ($part_size / (1024.0)));
		else
			$part_size_string = $part_size . " bytes";

                array_push($jsondata_size, gmp_intval($part_size));
                $part_label_string = $of_disks[$disk_index]["partitions"][$j]["device"];
                array_push($jsondata_label, substr($part_label_string, 5 , strlen($part_label_string)));


		print("<tr>\n");
		print("\t<td bgcolor=\"" . $dvalue . "\" align=\"center\">" . $of_disks[$disk_index]["partitions"][$j]["device"] . "</td>\n");
		print("\t<td bgcolor=\"" . $dvalue . "\" align=\"left\">" . $ptype_str . "</td>\n");
		print("\t<td bgcolor=\"" . $dvalue . "\" align=\"center\">" . $of_disks[$disk_index]["partitions"][$j]["number"] . "</td>\n");
		print("\t<td bgcolor=\"" . $dvalue . "\" align=\"center\">" . $of_disks[$disk_index]["partitions"][$j]["start"] . "</td>\n");
		print("\t<td bgcolor=\"" . $dvalue . "\" align=\"center\">" . $of_disks[$disk_index]["partitions"][$j]["end"] . "</td>\n");
		print("\t<td bgcolor=\"" . $dvalue . "\" align=\"center\">" . $of_disks[$disk_index]["partitions"][$j]["blocks"] . "</td>\n");
		print("\t<td bgcolor=\"" . $dvalue . "\" align=\"center\">" . $part_size_string . "</td>\n");
		print("\t<td bgcolor=\"" . $dvalue . "\" align=\"center\">" . ((($of_disks[$disk_index]["label"] == "gpt") || ($of_disks[$disk_index]["partitions"][$j]["number"] <= 4)) ? "Primary" : "Logical") . "</td>\n");

		print("\t<td bgcolor=\"" . $dvalue . "\" align=\"center\">");

		if ((($ptype != "8e") && ($ptype != "fd") && ($ptype != "05") && ($ptype != "5") && ($ptype != "0f") && ($ptype != "f")))
			print("-");
		else if ((($ptype == "05") || ($ptype == "5") || ($ptype == "0f") || ($ptype == "f")) && ($extended_partitions_exist))
			print("<em>Extended partitions exist</em>");
		else if ($in_use[$j])
			print("<em>使用中</em>");
		else
			print("<a href=\"./volumes_editpartitions.html?dev=" . htmlentities($dev) . "&amp;action=deletepartition&amp;device=" . $of_disks[$disk_index]["partitions"][$j]["device"] . "\">Delete</a>\n");

		print("</td>\n");

		print("</tr>\n");
	}

	print("</table>\n");
	print("</div>\n");

        $diskfree = $disksize - $sumpartsize;
        array_push($jsondata_size, (floatval($diskfree) > 4931584) ? $diskfree : 0); // is free space greater than 2MB?
        array_push($jsondata_label, "Free");


        print("<div><canvas id=\"graph\" height=\"200\" width=\"200\"></canvas></div>\n"); ?>
            <script src="/js/rendervolumegraph.js" type="text/javascript" language="JavaScript"></script>
            <script type="text/javascript">
                var jsondata_size = <?php print(json_encode($jsondata_size) . ";\n"); ?>
                var jsondata_label = <?php print(json_encode($jsondata_label) . ";\n"); ?>
                drawGraph(jsondata_size, jsondata_label);
            </script>
            <?php




	print("<p align=\"center\">&nbsp;</p>\n");

	print("<p align=\"center\"><a href=\"volumes_physical.html\">返回到存储设备列表</a></p>\n");

	print("<p align=\"center\">&nbsp;</p>\n");

	print("<hr />\n");

	print("<h3 align=\"center\">在 " . $of_disks[$disk_index]["device"] . " 建立分区</h3>\n");

	if ((($of_disks[$disk_index]["label"] == "gpt") && ($primaries < 32767) &&
			($primary_cylinder_count < $of_disks[$disk_index]["cylinders"])) ||
		(($primaries < 15) && ($primary_cylinder_count < $of_disks[$disk_index]["cylinders"])) ||
		(($extended_partition_index > -1) &&
		($extended_cylinder_count < ($of_disks[$disk_index]["partitions"][$extended_partition_index]["end"]
		- $of_disks[$disk_index]["partitions"][$extended_partition_index]["start"] + 1))))

	{
		printMessageBlock("info","<p>你可以在下面的范围内建立分区</p>\n");

		array_multisort($primary_start_array, $primary_end_array);
		array_multisort($extended_start_array, $extended_end_array);

		$primary_free_start_array = array();
		$primary_free_end_array = array();
		$extended_free_start_array = array();
		$extended_free_end_array = array();

		print("<div align=\"center\">\n");
		print("<table cellpadding=\"8\" cellspacing=\"2\" border=\"0\">\n");

		print("<tr>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>模式</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>开始柱面</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>结束柱面</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>空间</strong></td>\n");
		print("</tr>\n");

		if (((($of_disks[$disk_index]["label"] == "gpt") && ($primaries < 32767))
			|| ($primaries < 15)) && ($primary_cylinder_count < $of_disks[$disk_index]["cylinders"]))
		{
			$t = 1;

			for ($i = 0; $i < count($primary_start_array); $i++)
			{
				if ($primary_start_array[$i] > $t)
				{
					array_push($primary_free_start_array, $t);
					array_push($primary_free_end_array, ($primary_start_array[$i] - 1));

					$extent_size = (($of_disks[$disk_index]["blocksize"] / 2) * ((($primary_start_array[$i] - 1) - $t + 1) * $of_disks[$disk_index]["sectors"] * $of_disks[$disk_index]["heads"]));

					if ($extent_size > (1024 * 1024 * 1024))
						$extent_size_string = sprintf("%.02f GB", ($extent_size / (1024 * 1024 * 1024.0)));
					else if ($extent_size > (1024 * 1024))
						$extent_size_string = sprintf("%.02f MB", ($extent_size / (1024 * 1024.0)));
					else if ($extent_size > (1024))
						$extent_size_string = sprintf("%.02f KB", ($extent_size / (1024.0)));
					else
						$extent_size_string = $extent_size . " bytes";

					print("<tr>\n");
					print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">Primary</td>\n");
					print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">" . $t . "</td>\n");
					print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">" . ($primary_start_array[$i] - 1) . "</td>\n");
					print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">" . $extent_size_string . "</td>\n");
					print("</tr>\n");
				}

				$t = $primary_end_array[$i] + 1;
			}

			if ($t < $of_disks[$disk_index]["cylinders"])
			{
				array_push($primary_free_start_array, $t);
				array_push($primary_free_end_array, $of_disks[$disk_index]["cylinders"]);

				$extent_size = (($of_disks[$disk_index]["blocksize"] / 2) * (($of_disks[$disk_index]["cylinders"] - $t + 1) * $of_disks[$disk_index]["sectors"] * $of_disks[$disk_index]["heads"]));

				if ($extent_size > (1024 * 1024 * 1024))
					$extent_size_string = sprintf("%.02f GB", ($extent_size / (1024 * 1024 * 1024.0)));
				else if ($extent_size > (1024 * 1024))
					$extent_size_string = sprintf("%.02f MB", ($extent_size / (1024 * 1024.0)));
				else if ($extent_size > (1024))
					$extent_size_string = sprintf("%.02f KB", ($extent_size / (1024.0)));
				else
					$extent_size_string = $extent_size . " bytes";

				print("<tr>\n");
				print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">Primary</td>\n");
				print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">" . $t . "</td>\n");
				print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">" . $of_disks[$disk_index]["cylinders"] . "</td>\n");
				print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">" . $extent_size_string . "</td>\n");
				print("</tr>\n");
			}
		}

		if ((($extended_partition_index > -1) &&
			($extended_cylinder_count < ($of_disks[$disk_index]["partitions"][$extended_partition_index]["end"]
			- $of_disks[$disk_index]["partitions"][$extended_partition_index]["start"] + 1))))
		{
			$t = $of_disks[$disk_index]["partitions"][$extended_partition_index]["start"];

			for ($i = 0; $i < count($extended_start_array); $i++)
			{
				if ($extended_start_array[$i] > $t)
				{
					array_push($extended_free_start_array, $t);
					array_push($extended_free_end_array, ($extended_start_array[$i] - 1));

					$extent_size = (($of_disks[$disk_index]["blocksize"] / 2) * ((($extended_start_array[$i] - 1) - $t + 1) * $of_disks[$disk_index]["sectors"] * $of_disks[$disk_index]["heads"]));

					if ($extent_size > (1024 * 1024 * 1024))
						$extent_size_string = sprintf("%.02f GB", ($extent_size / (1024 * 1024 * 1024.0)));
					else if ($extent_size > (1024 * 1024))
						$extent_size_string = sprintf("%.02f MB", ($extent_size / (1024 * 1024.0)));
					else if ($extent_size > (1024))
						$extent_size_string = sprintf("%.02f KB", ($extent_size / (1024.0)));
					else
						$extent_size_string = $extent_size . " bytes";

					print("<tr>\n");
					print("\t<td bgcolor=\"" . $GLOBALS["color_table_row2"] . "\" align=\"center\">Logical</td>\n");
					print("\t<td bgcolor=\"" . $GLOBALS["color_table_row2"] . "\" align=\"center\">" . $t . "</td>\n");
					print("\t<td bgcolor=\"" . $GLOBALS["color_table_row2"] . "\" align=\"center\">" . ($extended_start_array[$i] - 1) . "</td>\n");
					print("\t<td bgcolor=\"" . $GLOBALS["color_table_row2"] . "\" align=\"center\">" . $extent_size_string . "</td>\n");
					print("</tr>\n");
				}

				$t = $extended_end_array[$i] + 1;
			}

			if ($t < $of_disks[$disk_index]["partitions"][$extended_partition_index]["end"])
			{
				array_push($extended_free_start_array, $t);
				array_push($extended_free_end_array, $of_disks[$disk_index]["partitions"][$extended_partition_index]["end"]);

				$extent_size = (($of_disks[$disk_index]["blocksize"] / 2) * (($of_disks[$disk_index]["partitions"][$extended_partition_index]["end"] - $t + 1) * $of_disks[$disk_index]["sectors"] * $of_disks[$disk_index]["heads"]));

				if ($extent_size > (1024 * 1024 * 1024))
					$extent_size_string = sprintf("%.02f GB", ($extent_size / (1024 * 1024 * 1024.0)));
				else if ($extent_size > (1024 * 1024))
					$extent_size_string = sprintf("%.02f MB", ($extent_size / (1024 * 1024.0)));
				else if ($extent_size > (1024))
					$extent_size_string = sprintf("%.02f KB", ($extent_size / (1024.0)));
				else
					$extent_size_string = $extent_size . " bytes";

				print("<tr>\n");
				print("\t<td bgcolor=\"" . $GLOBALS["color_table_row2"] . "\" align=\"center\">Logical</td>\n");
				print("\t<td bgcolor=\"" . $GLOBALS["color_table_row2"] . "\" align=\"center\">" . $t . "</td>\n");
				print("\t<td bgcolor=\"" . $GLOBALS["color_table_row2"] . "\" align=\"center\">" . $of_disks[$disk_index]["partitions"][$extended_partition_index]["end"] . "</td>\n");
				print("\t<td bgcolor=\"" . $GLOBALS["color_table_row2"] . "\" align=\"center\">" . $extent_size_string . "</td>\n");
				print("</tr>\n");
			}
		}

		print("</table>\n");
		print("</div>\n");
?>

<script language="JavaScript" type="text/javascript">
<!--

var primary_free_start_array = new Array();
var primary_free_end_array = new Array();
var extended_free_start_array = new Array();
var extended_free_end_array = new Array();
var size_multiplier = <?php print(($of_disks[$disk_index]["blocksize"] / 2) * $of_disks[$disk_index]["sectors"] * $of_disks[$disk_index]["heads"]); ?>;

<?php
	for ($i = 0; $i < count($primary_free_start_array); $i++)
	{
		print("primary_free_start_array[" . $i . "] = " . $primary_free_start_array[$i] . ";\n");
		print("primary_free_end_array[" . $i . "] = " . $primary_free_end_array[$i] . ";\n");
	}

	print("\n");

	for ($i = 0; $i < count($extended_free_start_array); $i++)
	{
		print("extended_free_start_array[" . $i . "] = " . $extended_free_start_array[$i] . ";\n");
		print("extended_free_end_array[" . $i . "] = " . $extended_free_end_array[$i] . ";\n");
	}

	print("\n");

?>

function update_size()
{

	extent_size = (document.createpartitionform.end.value - document.createpartitionform.start.value + 1) * size_multiplier;

	if (extent_size > (1024 * 1024 * 1024))
		extent_size_string = (Math.round((extent_size / (1024 * 1024 * 1024.0)) * 100) / 100) + " GB";
	else if (extent_size > (1024 * 1024))
		extent_size_string = (Math.round((extent_size / (1024 * 1024.0)) * 100) / 100) + " MB";
	else if (extent_size > (1024))
		extent_size_string = (Math.round((extent_size / (1024.0)) * 100) / 100) + " KB";
	else
		extent_size_string = extent_size + " bytes";

	document.createpartitionform.size.value = extent_size_string;

	if (extent_size < (32 * 1024 * 1024))
		document.createpartitionform.submit.disabled = true;
	else
		document.createpartitionform.submit.disabled = false;
}

function changed_mode()
{
	if (document.createpartitionform.mode.value == "logical")
	{
		document.createpartitionform.start.value = extended_free_start_array[0];
		document.createpartitionform.end.value = extended_free_end_array[0];
	}
	else
	{
		document.createpartitionform.start.value = primary_free_start_array[0];
		document.createpartitionform.end.value = primary_free_end_array[0];
	}

	update_size();
}

function changed_start()
{
	var i, found;

	if (document.createpartitionform.mode.value == "logical")
	{
		found = false;

		for (i = 0; i < extended_free_start_array.length; i++)
		{
			if ((extended_free_start_array[i] <= parseInt(document.createpartitionform.start.value)) &&
				(extended_free_end_array[i] >= parseInt(document.createpartitionform.start.value)))
			{
				found = true;
				break;
			}
		}

		if (!found)
		{
			changed_mode();
			return;
		}

		if ((parseInt(document.createpartitionform.start.value) > parseInt(document.createpartitionform.end.value)) ||
			(extended_free_end_array[i] < parseInt(document.createpartitionform.end.value)))
		{
			document.createpartitionform.end.value = extended_free_end_array[i];
		}
	}
	else
	{
		found = false;

		for (i = 0; i < primary_free_start_array.length; i++)
		{
			if ((primary_free_start_array[i] <= parseInt(document.createpartitionform.start.value)) &&
				(primary_free_end_array[i] >= parseInt(document.createpartitionform.start.value)))
			{
				found = true;
				break;
			}
		}

		if (!found)
		{
			changed_mode();
			return;
		}

		if ((parseInt(document.createpartitionform.start.value) > parseInt(document.createpartitionform.end.value)) ||
			(primary_free_end_array[i] < parseInt(document.createpartitionform.end.value)))
		{
			document.createpartitionform.end.value = primary_free_end_array[i];
		}
	}

	update_size();
}

function changed_end()
{
	changed_start();
}

// -->
</script>

<?php
		print("<p align=\"center\">&nbsp;</p>\n");

		print("<form name=\"createpartitionform\" method=\"post\" action=\"volumes_editpartitions.html\">\n");
		print("<input type=\"hidden\" name=\"action\" value=\"createpartition\" />\n");
		print("<input type=\"hidden\" name=\"dev\" value=\"" . $of_disks[$disk_index]["device"] . "\" />\n");

		print("<div align=\"center\">\n");
		print("<table cellpadding=\"8\" cellspacing=\"2\" border=\"0\">\n");

		print("<tr>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>模式</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>分区类型</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>开始柱面</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>结束柱面</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>大小</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>建立</strong></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_heading"] . "\" align=\"center\"><strong>重置</strong></td>\n");
		print("</tr>\n");

		print("<tr>\n");

		print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">\n");
		print("\t<select name=\"mode\" onchange=\"changed_mode()\">\n");

		if (((($of_disks[$disk_index]["label"] == "gpt") && ($primaries < 32767))
				|| ($primaries < 15)) && ($primary_cylinder_count < $of_disks[$disk_index]["cylinders"]))
			print("\t\t<option value=\"primary\">Primary</option>\n");

		if ((($extended_partition_index > -1) &&
			($extended_cylinder_count < ($of_disks[$disk_index]["partitions"][$extended_partition_index]["end"]
			- $of_disks[$disk_index]["partitions"][$extended_partition_index]["start"] + 1))))
			print("\t\t<option value=\"logical\">Logical</option>\n");

		print("\t</select>\n");
		print("\t</td>\n");

		print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">\n");
		print("\t<select name=\"type\">\n");

		if (($of_disks[$disk_index]["label"] != "gpt") && ((($primaries < 15) && ($primary_cylinder_count < $of_disks[$disk_index]["cylinders"]))) && ($extended_partition_index < 0))
			print("\t\t<option value=\"extended\">Extended partition</option>\n");

		print("\t\t<option value=\"pv\">Physical volume</option>\n");
		print("\t\t<option value=\"md\">RAID array member</option>\n");
		print("\t</select>\n");
		print("\t</td>\n");

		print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\"><input type=\"text\" name=\"start\" onchange=\"changed_start()\" size=\"10\" /></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\"><input type=\"text\" name=\"end\" onchange=\"changed_end()\" size=\"10\" /></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\"><input type=\"text\" name=\"size\" size=\"10\" disabled=\"disabled\" /></td>\n");
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Create\" /></td>\n");

		//print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\"<a href=\"./volumes_editpartitions.html?dev=" . htmlentities($dev) . "&amp;action=resetpartitions\">Reset</a></td>\n");

		$rand_id = sha1($dev . rand());
		print("\t<td bgcolor=\"" . $GLOBALS["color_table_row1"] . "\" align=\"center\">\n");
		print("\t\t<div id=\"popup-" . $rand_id . "\" onclick=\"event.cancelBubble = true;\" onmousedown=\"dragpopup(this, event)\" class=\"sharespopup\">\n");
		print("\t\t\t<p align=\"center\"><strong>Are you sure you want to reset<br />disk partitions from \"" . htmlentities($dev) . "\"?</strong></p>\n");
		print("\t\t\t<p align=\"center\">[&nbsp;<a href=\"./volumes_editpartitions.html?dev=" . htmlentities($dev) . "&amp;action=resetpartitions\">Yes</a> / <a href=\"#\" onclick=\"hideCurrentPopup(); return false;\">NO DON'T RESET</a>&nbsp;]</p>\n");
		print("\t\t</div>\n");
		if (count($in_use) > 0)
			print("\t\t<em>使用中</em>");
		else
			print("\t\t<a href=\"#\" onclick=\"return !showPopup('popup-" . $rand_id . "', event);\">重置</a>\n");
		print("\t</td>\n");

		print("</tr>\n");

		print("</table>\n");
		print("</div>\n");

		print("</form>\n");
?>

<script language="JavaScript" type="text/javascript">
<!--
	changed_mode();
// -->
</script>

<?php
	}
	else
	    printMessageBlock("warning", "<p align=\"center\"><em>Can't create any more partitions.</em></p>\n");

	print("<p align=\"center\">&nbsp;</p>\n");

	nested_tab_end();

	single_end(array());
	generic_footer(array());
?>
